﻿/************************************************************************
 * SMLib - State Model Library
 * Copyright (C) 2012-2013 Ali Khalili (khalili_DOT_ir_@SIGN_gmail_DOT_com)
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL) published 
 * by the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version. This program is distributed without any
 * warranty. See the GNU Lesser General Public License for more details. 
 * ***********************************************************************
 * File         :     SMLib\Automata\Base\PairOfSymbols.cs
 * Description  :     Implementation of Pair of Symbols. (it is somehow like KeyPairValue!)
 * Change log   :     -Oct 2013: First version
 * To Do        :
 ************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace SMLib.Automata.Base
{
    /// <summary>
    /// Implementation of Pair of Symbols. (it is somehow like KeyPairValue!)
    /// </summary>
    public class PairOfSymbols
    {

        /// <summary>
        /// The input symbol
        /// </summary>
        protected ISymbol input;

        /// <summary>
        /// The output symbol
        /// </summary>
        protected ISymbol output;

        /// <summary>
        /// Not accessiable from outside. Just for childs to build their own costomized pair
        /// </summary>
        protected PairOfSymbols()
        {
            //* nothing
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="input">input symbol</param>
        /// <param name="output">output symbol</param>
        public PairOfSymbols(ISymbol input, ISymbol output)
        {
            this.input = input;
            this.output = output;
        }

        #region PairOfSymbols Members
        /// <summary>
        /// input symbol of the pair
        /// </summary>
        public ISymbol Input
        {
            get { return input; }
        }

        /// <summary>
        /// output symbol of the pair
        /// </summary>
        public ISymbol Output
        {
            get { return output; }
        }

        #endregion

        /// <summary>
        /// Checking equality of two pair of symbols
        /// </summary>
        /// <param name="obj">the object to be compared with</param>
        /// <returns>true if it is equal to obj, false if not</returns>
        public override bool Equals(object obj)
        {
            if (obj is PairOfSymbols)
            {
                PairOfSymbols o = (PairOfSymbols)obj;
                if (input != null && output != null)
                    return (input.Equals(o.Input) && output.Equals(o.Output));
                else
                {
                    if (input == null && output == null)
                    {
                        return (o.Input == null && o.Output == null);
                    }
                    else if (input == null)
                    {
                        return output.Equals(o.Output);
                    }
                    else
                        return input.Equals(o.Input);
                }

            }
            return false;
        }

        /// <summary>
        /// Get hash generatedCode for this pair of symbols
        /// </summary>
        /// <returns>hash generatedCode of the pair</returns>
        public override int GetHashCode()
        {
            return input.GetHashCode() + output.GetHashCode();
        }

        /// <summary>
        /// Converting the pair to string. Used in presentation
        /// </summary>
        /// <returns>the corresponding string value</returns>
        public override string ToString()
        {
            return "(" + (input == null ? "NULL" : input.ToString()) + "/" + (output == null ? "NULL" : output.ToString()) + ")";
        }


    }


}
